//
//  MyCollectionViewCell.m
//  ZAKER
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MyCollectionViewCell.h"

#define CellCount 3

@interface MyCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@end


@implementation MyCollectionViewCell

-(void)dealloc
{
    
    [_tableView release];
    [_modelArray release];
    
    [super dealloc];
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.modelArray = [NSMutableArray array];
        
    }
    
    return self;
    
}

#pragma mark - 创建tableView, 以及实现协议方法

-(void)createTableView
{
    self.tableView = [[[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease];
    _tableView.rowHeight = 180 * myHeight;
   _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.contentView addSubview:_tableView];
//    [_tableView release];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return CellCount;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.numItem * CellCount + indexPath.row < _modelArray.count) {
        
        CustomTableViewCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        if (customCell == nil) {
            
            customCell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
            
        }
        
    CellModel *model = [_modelArray objectAtIndex:(CellCount * self.numItem) + indexPath.row];
        
    [customCell.myImage sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"pla.jpg"]];
    customCell.titleLabel.text = model.title;
        
        // 判断如果数组为空
        if ([model.taginfo isEqual:@""]) {
        } else {
            
            NSArray *array = model.taginfo;
            NSDictionary *dic = array[0];
            NSString *str = dic[@"name"];
            customCell.nameLabel.text = str;
            
        }
        
        NSInteger time = (  [model.update_time integerValue] - [model.publishtime integerValue]) / 3600;
        if (time < 0) {
            
            time = -time;
            customCell.timeLabel.text = [NSString stringWithFormat:@"%ld小时前",time ];
            
        } else if (time == 0){
            
            customCell.timeLabel.text = @"刚刚更新" ;
            
        } else {
            
            customCell.timeLabel.text = [NSString stringWithFormat:@"%ld小时前", time];
            
        }
        customCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return customCell;
        
    }
    
    return [[UITableViewCell alloc]init];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return self.frame.size.height / 3;
    
}

@end
